package cc.wanforme.chipmunity.common;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.util.DigestUtils;

import cc.wanforme.chipmunity.common.consts.EncodeEnum;

/**
 * @author wanne
 * 2019年8月13日
 * 
 */
public class TokenUtil {
	//private static final String SALT = "cm";
	
	public static String createToken(Map<String, String> map) {
		StringBuffer sb = new StringBuffer("");
		Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry<String, String> next = iterator.next();
			sb.append(next.getKey()+"-"+next.getValue()+",");
		}
		
		/** 最后面是随机数*/
		sb.append(System.currentTimeMillis()%1000000);
		
		return encript(sb.toString());
		// return BCrypt.hashpw(sb.toString(), SALT);
	}
	
	public static String createToken(String...args) {
		StringBuffer sb = new StringBuffer("");
		for (String str : args) {
			sb.append(str+",");
		}
		
		/** 最后面是随机数*/
		sb.append(System.currentTimeMillis()%1000000);
		return encript(sb.toString());
		//return BCrypt.hashpw(sb.toString(), SALT);
	}
	
	
	private static String encript(String str) {
		try {
			return DigestUtils.md5DigestAsHex(str.toString()
					.getBytes(EncodeEnum.STRING_TO_BYTES.getName()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str.toString();
		}
	}
}
